<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>

    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/setting'
import { getUserInfoById, assignRoles } from '@/api/user'
export default {
  props: {
    userId: String,
    required: true
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  // created() {
  //   this.loadRoles()
  // },
  methods: {
    // 点击确定,发送请求
    async hSubmit() {
      const res = await assignRoles({ id: this.userId, roleIds: this.roleIds })
      console.log(res, 'res')
      // 子传父
      this.$emit('success')
      // 给提示
      this.$message.success('宝宝真棒,分配成功啦~')
    },
    // 获取所有角色
    async loadRoles() {
      try {
        const { data } = await getRoles({ page: 1, pagesize: 100 })
        console.log('获取所有角色', data)
        const roleIds = await getUserInfoById(this.userId)
        console.log(roleIds, 'roleIds')
        // 保存数据
        this.roleIds = roleIds.data.roleIds
        this.list = data.rows
      } catch (err) {
        console.log('getRoles', err)
      }
    },
    closeDialog() {
      this.$emit('close')
    }
  }
}
</script>
